﻿using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

// Toolkit namespace
using SimpleMvvmToolkit;

namespace ToolRental.Client.SMvvm.Admin
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class BorrowViewModel : ViewModelBase<BorrowViewModel>, INotifyError
    {
        private IBorrowServiceAgent serviceAgent;

        #region Constructor
        public BorrowViewModel() { }

        public BorrowViewModel(IBorrowServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            RefreshList();
        }
        #endregion

        #region RefreshList
        public async void RefreshList()
        {
            var borrows = await serviceAgent.GetBorrowDeviceList(this);
            BorrowList = new ObservableCollection<Borrow>(borrows);

            var orders = await serviceAgent.GetOrderDeviceList(this);
            OrderList = new ObservableCollection<Order>(orders);
        }
        #endregion

        #region StartBorrow
        public void StartBorrow()
        {
            if (SelectedOrder != null)
            {
                if (NewBorrowSelectedItem != null)
                {
                    serviceAgent.StartBorrow(SelectedOrder.Id, NewBorrowSelectedItem.Id, NewBorrowDeadline, this);
                    RefreshList();
                }
                else
                {
                    ErrorOccured("Válassza ki a készüléket!", null);
                }
            }
            else
            {
                ErrorOccured("Válasszon ki egy megrendelést!", null);
            }
        }
        #endregion

        #region StopBorrow
        public void StopBorrow()
        {
            if (SelectedBorrow != null)
            {
                serviceAgent.FinishBorrow(SelectedBorrow.Id, this);
                RefreshList();
            }
            else
            {
                ErrorOccured("Nincs kiválasztva kölcsönzés!", null);
            }
        }
        #endregion

        #region BorrowList
        private ObservableCollection<Borrow> borrowList;
        public ObservableCollection<Borrow> BorrowList
        {
            get { return borrowList; }
            set
            {
                borrowList = value;
                NotifyPropertyChanged(bl => bl.BorrowList);
            }
        }
        #endregion  

        #region OrderList
        private ObservableCollection<Order> orderList;
        public ObservableCollection<Order> OrderList
        {
            get { return orderList; }
            set
            {
                orderList = value;
                NotifyPropertyChanged(o => o.OrderList);
            }
        }
        #endregion

        #region OrderSelectionChanged
        public async void OrderSelectionChanged()
        {
            if (SelectedOrder != null)
            {
                var devices = await serviceAgent.GetSelectedOrderDeviceList(SelectedOrder.DeviceTypeId, this);
                NewBorrowDevices = new ObservableCollection<Device>(devices);
            }
            else
            {
                NewBorrowDevices = new ObservableCollection<Device>();
            }
        }
        #endregion

        #region NotifyError

        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;
        private void NotifyError(string message, Exception error)
        {
            // Notify view of an error
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }

        #endregion

        #region  ErrorOccured
        public void ErrorOccured(string message, Exception exception)
        {
            MessageBox.Show(message);
        }
        #endregion

        #region NewBorrowDeadline
        private DateTime newBorrowDeadline = DateTime.Now;
        public DateTime NewBorrowDeadline
        {
            get { return newBorrowDeadline; }
            set
            {
                newBorrowDeadline = value;
                NotifyPropertyChanged(m => m.NewBorrowDeadline);
            }
        }
        #endregion

        #region NewBorrowDevices
        private ObservableCollection<Device> newBorrowDevices;
        public ObservableCollection<Device> NewBorrowDevices
        {
            get { return newBorrowDevices; }
            set
            {
                newBorrowDevices = value;
                NotifyPropertyChanged(m => m.NewBorrowDevices);
            }
        }
        #endregion

        #region NewBorrowSelectedItem
        private Device newBorrowSelectedItem;
        public Device NewBorrowSelectedItem
        {
            get { return newBorrowSelectedItem; }
            set
            {
                newBorrowSelectedItem = value;
                NotifyPropertyChanged(m => m.NewBorrowSelectedItem);
            }
        }
        #endregion

        #region SelectedBorrowIndex
        private int selectedBorrowIndex;
        public int SelectedBorrowIndex
        {
            get { return selectedBorrowIndex; }
            set
            {
                selectedBorrowIndex = value;
                NotifyPropertyChanged(m => m.SelectedBorrowIndex);
            }
        }
        #endregion

        #region SelectedBorrow
        private Borrow selectedBorrow;
        public Borrow SelectedBorrow
        {
            get { return selectedBorrow; }
            set
            {
                selectedBorrow = value;
                NotifyPropertyChanged(m => m.SelectedBorrow);
            }
        }
        #endregion

        #region SelectedOrderIndex
        private int selectedOrderIndex;
        public int SelectedOrderIndex
        {
            get { return selectedOrderIndex; }
            set
            {
                selectedOrderIndex = value;
                NotifyPropertyChanged(m => m.SelectedOrderIndex);
            }
        }
        #endregion

        #region SelectedOrder
        private Order selectedOrder;
        public Order SelectedOrder
        {
            get { return selectedOrder; }
            set
            {
                selectedOrder = value;
                NotifyPropertyChanged(m => m.SelectedOrder);
            }
        }
        #endregion
    }
}